import 'package:date_format/date_format.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:imperius_insights/modules/projects/viewmodels/project.dart';
import 'package:imperius_insights/portal/widgets/responsive-widget.dart';
import 'package:imperius_insights/routes/navigator.dart';
import 'package:imperius_insights/routes/paths.dart';
import 'package:imperius_insights/treaty/dimensions.dart';
import 'package:imperius_insights/treaty/palette.dart';
import 'package:imperius_insights/treaty/styles.dart';
import 'package:url_launcher/url_launcher.dart';

import 'quality.dart';

typedef OnProjectClicked = void Function(ProjectViewModel project);

class ProjectGlance extends StatelessWidget {
  final ProjectViewModel project;
  final OnProjectClicked onClicked;
  final double topPadding;
  final double bottomPadding;
  final bool hideQualityLabel;
  final bool showScheduledDatePoint;

  ProjectGlance(this.project, {this.onClicked, this.topPadding = 0, this.bottomPadding: 20, this.hideQualityLabel = false, this.showScheduledDatePoint = true});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        SizedBox(
          height: topPadding,
        ),
        Material(
            elevation: 6,
            borderRadius: BorderRadius.circular(4),
            color: Colors.white,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                InkWell(
                  onTap: () => this.onClicked(project),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                          decoration: BoxDecoration(color: project.themeColor),
                          padding: EdgeInsets.symmetric(
                              horizontal:
                                  layoutDimensions.cardPaddingHorizontal,
                              vertical: 8),
                          child: Row(
                            children: <Widget>[
                              Expanded(
                                child: Text('${project.cardTitle}',
                                    style: styles.contentContrast),
                              )
                            ],
                          )),
                      Container(
                        padding: EdgeInsets.fromLTRB(
                            layoutDimensions.cardPaddingHorizontal,
                            layoutDimensions.cardPaddingVertical,
                            layoutDimensions.cardPaddingHorizontal,
                            layoutDimensions.cardPaddingVerticalHalf),
                        child:
                            Text('项目名：${project.name}', style: styles.content),
                      ),
                      project.humansPMLabel.length > 0
                          ? Container(
                              padding: EdgeInsets.symmetric(
                                  horizontal:
                                      layoutDimensions.cardPaddingHorizontal,
                                  vertical:
                                      layoutDimensions.cardPaddingVerticalHalf),
                              child: Text('产品经理：${project.humansPMLabel}',
                                  style: styles.content),
                            )
                          : Container(),
                      project.humansRDLabel.length > 0
                          ? Container(
                              padding: EdgeInsets.symmetric(
                                  horizontal:
                                      layoutDimensions.cardPaddingHorizontal,
                                  vertical:
                                      layoutDimensions.cardPaddingVerticalHalf),
                              child: Text('开发人员：${project.humansRDLabel}',
                                  style: styles.content),
                            )
                          : Container(),
                      project.humansQALabel.length > 0
                          ? Container(
                              padding: EdgeInsets.fromLTRB(
                                  layoutDimensions.cardPaddingHorizontal,
                                  layoutDimensions.cardPaddingVerticalHalf,
                                  layoutDimensions.cardPaddingHorizontal,
                                  layoutDimensions.cardPaddingVertical),
                              child: Text('QA：${project.humansQALabel}',
                                  style: styles.content),
                            )
                          : Container(),
                      buildDateArea(),
//                      Divider(height: 1, color: Colors.grey[200]),
                      /// 项目数据
                    ],
                  ),
                ),
                Divider(thickness: 0.5, height: 0, color: palette.divider),
                !hideQualityLabel ? (ResponsiveWidget.isLandscape(context)
                    ? Row(
                        children: <Widget>[
                          Expanded(
                            child: Container(
                              decoration: BoxDecoration(
                                  border: Border(
                                      right: BorderSide(
                                          width: 0.5, color: palette.divider))),
                              child: InkWell(
                                onTap: () => launch(project.jiraLink),
                                child: Center(
                                  child: Container(
                                    padding: EdgeInsets.all(
                                        layoutDimensions.cardPaddingAll),
                                    child:
                                        Text('查看JIRA', style: styles.content),
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              decoration: BoxDecoration(
                                  border: Border(
                                      right: BorderSide(
                                          width: 0.5, color: palette.divider))),
                              child: InkWell(
                                onTap: () => launch(project.bugsLink),
                                child: Center(
                                  child: Container(
                                    padding: EdgeInsets.all(
                                        layoutDimensions.cardPaddingAll),
                                    child:
                                        Text('查看Bugs', style: styles.content),
                                  ),
                                ),
                              ),
                            ),
                          ),
                          Expanded(
                            child: Container(
                              decoration: BoxDecoration(
                                  border: Border(
                                      right: BorderSide(
                                          width: 0.5, color: palette.divider))),
                              child: InkWell(
                                onTap: () => navigateTo(
                                    context, Paths.pmoQuality,
                                    params: {'pmo': project.pmo},
                                    hostMode: CompactHostMode.Outside),
                                child: Center(
                                  child: Container(
                                    padding: EdgeInsets.all(
                                        layoutDimensions.cardPaddingAll),
                                    child: Text('质量分析', style: styles.content),
                                  ),
                                ),
                              ),
                            ),
                          )
                        ],
                      )
                    : Container()) : Container(),
                ResponsiveWidget.isLandscape(context)
                    ? Divider(thickness: 0.5, height: 0, color: palette.divider)
                    : Container(),
                hideQualityLabel ? Container() : InkWell(
                  onTap: () => navigateTo(
                      context, Paths.pmoQuality,
                      params: {'pmo': project.pmo},
                      hostMode: CompactHostMode.Outside),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: buildQualityLabel(context, project.data),
                      )
                    ],
                  ),
                ),
                buildNoticeLabel(
                    project.delayInfo, () => launch(project.jiraLink)),
                buildNoticeLabel(project.hiatus, () => launch(project.jiraLink))
              ],
            )),
        SizedBox(
          height: bottomPadding,
        ),
      ],
    );
  }

  buildNoticeLabel(String notice, Function onClickHandler) {
    if (notice == null || notice == '') {
      return Container();
    }
    return Column(
      children: <Widget>[
        Divider(thickness: 0.5, height: 0),
        InkWell(
            onTap: onClickHandler,
            child: Container(
                color: Colors.red,
                padding: EdgeInsets.symmetric(
                    vertical: layoutDimensions.cardPaddingVertical,
                    horizontal: layoutDimensions.cardPaddingHorizontal),
                child: Row(
                  children: <Widget>[
                    Icon(Icons.info, size: 18, color: Colors.white),
                    Padding(padding: EdgeInsets.all(4)),
                    Expanded(
                        child: Text(notice,
                            style:
                                TextStyle(color: Colors.white, fontSize: 12)))
                  ],
                )))
      ],
    );
  }

  Widget buildDateArea() {
    if (!showScheduledDatePoint) {
      return Container();
    }
    final projectData = project.data;
    final widgets = <Widget>[];
    if (projectData.startDevDate != null) {
      widgets.add(buildLabelInfoArea(
          '计划开始开发日期：${formatDate(projectData.startDevDate, [
        yyyy,
        '-',
        mm,
        '-',
        dd
      ])}'));
    }
    if (projectData.endDevDate != null) {
      widgets.add(buildLabelInfoArea(
          '计划开发完成日期：${formatDate(projectData.endDevDate, [
        yyyy,
        '-',
        mm,
        '-',
        dd
      ])}'));
    }
    if (projectData.startTestDate != null) {
      widgets.add(buildLabelInfoArea(
          '计划开始测试日期：${formatDate(projectData.startTestDate, [
        yyyy,
        '-',
        mm,
        '-',
        dd
      ])}'));
    }
    if (projectData.endTestDate != null) {
      widgets.add(buildLabelInfoArea(
          '计划测试完成日期：${formatDate(projectData.endTestDate, [
        yyyy,
        '-',
        mm,
        '-',
        dd
      ])}'));
    }
    if (projectData.actualPublishDate != null) {
      widgets.add(buildLabelInfoArea(
          '实际发布日期：${formatDate(projectData.actualPublishDate, [
        yyyy,
        '-',
        mm,
        '-',
        dd
      ])}'));
    } else if (projectData.scheduledPublishDate != null) {
      widgets.add(buildLabelInfoArea(
          '计划发布日期：${formatDate(projectData.scheduledPublishDate, [
        yyyy,
        '-',
        mm,
        '-',
        dd
      ])}'));
    }
    return Column(
        crossAxisAlignment: CrossAxisAlignment.start, children: widgets);
  }

  Widget buildLabelInfoArea(String text) {
    return Container(
      padding: EdgeInsets.fromLTRB(
          layoutDimensions.cardPaddingHorizontal,
          layoutDimensions.cardPaddingVerticalHalf,
          layoutDimensions.cardPaddingHorizontal,
          layoutDimensions.cardPaddingVertical),
      child: Text(text, style: styles.content),
    );
  }
}
